package com.sws.worktive;

import com.sws.worktive.util.Constant;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class HomeScreen extends Fragment implements OnClickListener {

	ImageView btn_view_post;
	boolean selection=false;
	Fragment currentFragmnet;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.home_screen, container, false);

		view.findViewById(R.id.icon_profile_header).setOnClickListener(this);
		selection = false;
		btn_view_post = (ImageView) view.findViewById(R.id.btn_view_post);
		btn_view_post.setOnClickListener(this);
		btn_view_post.setSelected(selection);
		
		view.findViewById(R.id.icon_search_header).setOnClickListener(this);
		
		setViewPostFragment(selection);
		
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.icon_profile_header:
			((DrawerScreen) getActivity()).drawerLayout
					.openDrawer(((DrawerScreen) getActivity()).mLeftDrawerRoot);
			break;

		case R.id.icon_search_header:

			((DrawerScreen) getActivity()).drawerLayout
					.openDrawer(((DrawerScreen) getActivity()).mRightDrawerRoot);
			break;

		case R.id.btn_view_post:
			if (selection) {
				btn_view_post.setSelected(true);
				setViewPostFragment(selection);
				selection = false;
			} else {
				btn_view_post.setSelected(false);
				setViewPostFragment(selection);
				selection = true;
			}

			break;
		default:
			break;
		}

	}

	public void setViewPostFragment(boolean selection) {

		if (selection) {
			FragmentManager fragmentManager = getChildFragmentManager();

			currentFragmnet = fragmentManager
					.findFragmentByTag(Constant.POST_FRAGMENT);

			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			if (currentFragmnet == null) {
				currentFragmnet = new PostScreen();
			}
			transaction.replace(R.id.home_container, currentFragmnet,
					Constant.POST_FRAGMENT);
			transaction.commit();

		} else {
			FragmentManager fragmentManager = getChildFragmentManager();
			currentFragmnet = fragmentManager
					.findFragmentByTag(Constant.VIEW_FRAGMENT);
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			if (currentFragmnet == null) {
				currentFragmnet = new ViewScreen();
			}

			transaction.replace(R.id.home_container, currentFragmnet,
					Constant.VIEW_FRAGMENT);
			transaction.commit();
		}
	}
}
